<?php

namespace apiend\modules\user\models;

use Yii;
use yii\data\ActiveDataProvider;
use common\helpers\Url;

/**
 * This is the model class for table "{{%dynamic}}".
 *
 * @property int $id
 * @property string $user_id 用户ID
 * @property string $content
 * @property int $direction 动态类型：0主动动态，1被动动态
 * @property int $retweet_id 转发动态原始ID
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 */
class MineCourseSearch extends \common\models\course\Course {

    /**
     * 浏览历史
     * @param array $uid 检索参数
     * @return ActiveDataProvider
     */
    public function search($uid) {
        $query = static::find()
        ->joinWith('order o')
        ->andWhere(['o.user_id' => $uid]);
        $query->orderBy([
            'o.pay_time' => SORT_DESC,
            'o.created_at' => SORT_DESC,
        ]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query
        ]);

//        return \common\helpers\Debug::sql($query);

        return $dataProvider;
    }

    public function fields() {
        return [
            'id',
            'name',
            'thumb',
            'begintime' => function () {
                return date('Y-m-d', $this->begin_time);
            },
            'teacher' => function () {
                if ($this->teacher) {
                    return $this->teacher->name;
                }
                return '待定';
            },
            'price',
            'finished',
            'order',
            'link' => function () {
                return Url::h5('/course/' . $this->id);
            },
        ];
    }

}
